package ycl.system.utils;

import com.github.xiaoymin.knife4j.core.util.StrUtil;
import ycl.common.utils.JSONObjectConvertUtil;
import ycl.system.entity.SysUser;
import ycl.system.res.SysUserRes;

/**
 * 用户隐私工具
 *
 * @author ycl
 * @date 2022/4/17 0017 21:12:11
 */
public class SysUserPrivacyUtil {


	/**
	 * 隐藏隐私
	 *
	 * @param src 原用户数据
	 * @return 隐藏后的数据
	 */
	public static SysUser hidePrivacy(SysUser src) {
		if (src == null)
			return new SysUser();
		SysUserRes data = JSONObjectConvertUtil.convert(src, SysUserRes.class);
		String idCard = data.getIdCard();
		if (StrUtil.isNotBlank(idCard))
			data.setIdCard(hideNumber(idCard));

		String mobile = data.getMobile();
		if (StrUtil.isNotBlank(mobile))
			data.setMobile(hideNumber(mobile));

		String realName = data.getRealName();
		if (StrUtil.isNotBlank(realName))
			data.setRealName(hideName(realName));
		return JSONObjectConvertUtil.convert(data, SysUser.class);
	}

	private static String hideName(String name) {
		StringBuilder b = new StringBuilder();
		char[] chars = name.toCharArray();
		int length = chars.length;
		for (int i = 0; i < length; i++) {
			if (length - i == 1)
				b.append(chars[i]);
			else
				b.append("*");
		}
		return b.toString();
	}

	private static String hideNumber(String num) {
		StringBuilder b = new StringBuilder();
		char[] chars = num.toCharArray();
		int length = chars.length;
		for (int i = 0; i < length; i++) {
			if (i == 0 || i == 1 || length - i == 1 || length - i == 2)
				b.append(chars[i]);
			else
				b.append("*");
		}
		return b.toString();
	}
}
